package hbt;

import org.hibernate.Session;
import org.hibernate.Transaction;

import repoDAO.ClienteDO;
import repositorio.*;

import org.hibernate.SessionFactory;

public class HibernateDAO {
	protected static HibernateDAO instancia = null;
	protected static SessionFactory sf = null;
	protected static Session session = null;

	
	public static HibernateDAO getInstancia(){
		if(instancia == null){
			instancia = new HibernateDAO();
		} 
		System.out.println("wrong DAO");
		return instancia;
	}
	
	protected HibernateDAO()  {
		sf = HibernateFactory.getSessionFactory();
	}

	public Session getSession(){
		if(session == null || !session.isOpen()){
			session = sf.openSession();
			System.out.println("OPEN: " + session.isOpen());
		}
		return session;
	}
	
	public void closeSession(){
		if (session.isOpen()) {
			session.close();
			System.out.println("\nOPEN: " + session.isOpen());
		}
	}
	
	
	public void persistCliente(ClienteTO cliente) {
		System.out.println("Entro al DAO");
		ClienteDO clienteloc = new ClienteDO(null, null, null, null, null, null, null, null, null);
		clienteloc.setApellido(cliente.getApellido());		
		clienteloc.setDomicilio(cliente.getDomicilio());
		clienteloc.setNombre(cliente.getNombre());
		clienteloc.setNroCliente(cliente.getNroCliente());
		clienteloc.setNroDoc(cliente.getNroDoc());
		clienteloc.setNroObraSocial(cliente.getNroObraSocial());
		clienteloc.setPlanGimnasio(cliente.getPlanGimnasio());
		clienteloc.setSexo(cliente.getSexo());
		clienteloc.setTelefono(cliente.getTelefono());
		System.out.println(clienteloc.toString());
		this.getSession().flush();
		Transaction tran = this.getSession().beginTransaction();
		this.getSession().persist(clienteloc);
		tran.commit();
		this.closeSession();
		
		
	}
	

}
